<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :width="dialogWidth"
    class="student-dialog"
    @close="$emit('on-close')"
    :lock-scroll="false"
    append-to-body
  >
    <!-- 内容容器，控制滚动区域 -->
    <div class="dialog-content-wrapper">
      <el-tabs v-model="activeTab" type="card" class="tabs-container">
        <!-- 基础信息 -->
        <el-tab-pane label="基础信息" name="base">
          <el-form
            ref="baseForm"
            :model="formData"
            :rules="baseRules"
            label-width="100px"
            class="form-grid"
          >
            <el-row :gutter="20" :xs="1" :sm="1" :md="2" :lg="2" :xl="2">
              <el-col :span="12">
                <el-form-item label="姓名" prop="name">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.name" placeholder="姓名"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.name || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="手机号" prop="phone">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.phone" placeholder="手机号"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.phone || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="地区">
                  <template v-if="isEditMode">
                    <el-cascader
                      v-model="formData.regionArray"
                      :options="regionCascaderOptions"
                      placeholder="请选择地区"
                      @change="handleRegionChange"
                      :popper-append-to-body="false"
                      style="width: 100%"
                    />
                  </template>
                  <template v-else>
                    <el-tag type="info">
                      {{ formData.regionArray[1]}}
                    </el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="年级">
                  <template v-if="isEditMode">
                    <el-select v-model="formData.grade" placeholder="选择年级" style="width: 100%">
                      <el-option
                        v-for="item in gradeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.grade || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="学校">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.school" placeholder="学校"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.school || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="推荐码">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.recommendCode" placeholder="推荐码"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.recommendCode || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="密码">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.password" placeholder="密码"/>
                    <div class="form-hint">提示：初始密码为 123456</div>
                  </template>
                  <template v-else>
                    <el-tag type="info">******</el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="性别">
                  <template v-if="isEditMode">
                    <el-radio-group v-model="formData.gender">
                      <el-radio :label="'0'">未知</el-radio>
                      <el-radio :label="'1'">男</el-radio>
                      <el-radio :label="'2'">女</el-radio>
                    </el-radio-group>
                  </template>
                  <template v-else>
                    <el-tag type="info">
                      {{
                        formData.gender === '1'
                          ? '男'
                          : formData.gender === '2'
                            ? '女'
                            : '未知'
                      }}
                    </el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>

        <!-- 拓展信息 -->
        <el-tab-pane label="拓展信息" name="ext">
          <el-form ref="extForm" :model="formData" label-width="100px" class="form-container">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="语文成绩">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.scoreChinese" placeholder="语文成绩" type="number"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.scoreChinese || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="数学成绩">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.scoreMath" placeholder="数学成绩" type="number"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.scoreMath || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="英语成绩">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.scoreEnglish" placeholder="英语成绩" type="number"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.scoreEnglish || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="物理成绩">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.scorePhysics" placeholder="物理成绩" type="number"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.scorePhysics || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="化学成绩">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.scoreChemistry" placeholder="化学成绩" type="number"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.scoreChemistry || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="爱好">
                  <template v-if="isEditMode">
                    <el-input v-model="formData.hobby" placeholder="多个爱好用逗号分隔"/>
                  </template>
                  <template v-else>
                    <el-tag type="info">{{ formData.hobby || '-' }}</el-tag>
                  </template>
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item label="其它信息">
              <template v-if="isEditMode">
                <el-input
                  v-model="formData.extraInfo"
                  type="textarea"
                  rows="4"
                  placeholder="其它信息"
                />
              </template>
              <template v-else>
                <el-tag type="info">{{ formData.extraInfo || '-' }}</el-tag>
              </template>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 底部按钮 -->
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('on-close')">取消</el-button>
      <template v-if="!isViewMode">
        <el-button type="primary" :style="{backgroundColor: themeColor, borderColor: themeColor}" @click="submitForm">
          确定
        </el-button>
      </template>
      <template v-if="isViewMode">
        <el-button
          type="primary"
          :style="{backgroundColor: themeColor, borderColor: themeColor}"
          @click="enterEditMode"
        >
          编辑
        </el-button>
      </template>
    </div>
  </el-dialog>
</template>

<script>
import { pcTree } from '@/api/commonApi'
import { saveOrUpdate } from '@/api/student'

export default {
  name: 'StudentDialog',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      required: true
    },
    formData: {
      type: Object,
      required: true
    },
    isViewMode: {
      type: Boolean,
      required: true
    },
    isEditMode: {
      type: Boolean,
      required: true
    },
    activeTab: {
      type: String,
      required: true
    },
    themeColor: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      baseRules: {
        name: [
          { required: true, message: '姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '正确的手机号', trigger: 'blur' }
        ]
      },
      dialogWidth: '80%',
      // 可配置的最大高度
      maxDialogHeight: 600, // 单位：px
      // 地区级联选项
      regionCascaderOptions: [],
      gradeOptions: []
    }
  },
  mounted() {
    this.calcDialogWidth()
    window.addEventListener('resize', this.calcDialogWidth)
    pcTree().then(res => {
      this.regionCascaderOptions = res
    })
    const gradeOptionArr = ['学前教育', '一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '初一', '初二', '初三', '高一', '高二', '高三', '大一', '大二', '大三', '大四', '成人教育', '其它']
    this.gradeOptions = gradeOptionArr.map(item => {
      return {
        label: item,
        value: item
      }
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcDialogWidth)
  },
  methods: {
    handleRegionChange(value) {
      this.$emit('on-region-change', value)
    },

    submitForm() {
      this.$refs.baseForm.validate(async valid => {
        if (valid) {
          await saveOrUpdate(this.formData)
          this.$notify.success({ title: '成功', message: this.formData.id ? '编辑成功' : '新增成功' })
          if (this.formData.id){
            this.dialogVisible = false
          }else {
            this.$emit('on-close')
          }
          this.$emit('reload')
        } else {
          return false
        }
      })
    },

    calcDialogWidth() {
      const screenWidth = window.innerWidth
      if (screenWidth < 768) {
        this.dialogWidth = '90%'
      } else if (screenWidth < 1200) {
        this.dialogWidth = '80%'
      } else {
        this.dialogWidth = '800px'
      }
    },

    // 进入编辑模式
    enterEditMode() {
      this.isViewMode = false
      this.isEditMode = true
      this.dialogTitle = '编辑学生'
    }
  }
}
</script>

<style scoped>
/* 限制对话框最大高度 */
.student-dialog ::v-deep(.el-dialog) {
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  max-width: 95vw;
  /* 限制对话框整体最大高度 */
  max-height: 80vh; /* 占视口高度的80% */
  display: flex;
  flex-direction: column;
  margin: 0 auto !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
}

.student-dialog ::v-deep(.el-dialog__header) {
  border-bottom: 1px solid #f0f0f0;
  flex-shrink: 0; /* 头部不收缩 */
}

/* 内容区域高度限制 */
.dialog-content-wrapper {
  flex: 1;
  /* 内容区最大高度 = 对话框最大高度 - 头部高度 - 底部高度 */
  max-height: calc(80vh - 130px);
  overflow-y: auto; /* 内容超出时显示滚动条 */
  scrollbar-width: thin;
  scrollbar-color: #ccc transparent;
}

.dialog-content-wrapper::-webkit-scrollbar {
  width: 6px;
}

.dialog-content-wrapper::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background-color: #ccc;
}

.dialog-content-wrapper::-webkit-scrollbar-track {
  background: transparent;
}

.tabs-container {
  width: 100%;
}

.form-grid, .form-container {
  width: 100%;
}

.form-hint {
  color: #909399;
  font-size: 12px;
  margin-top: 4px;
}

/* 底部区域固定 */
.dialog-footer {
  padding: 12px 0;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  flex-shrink: 0; /* 底部不收缩 */
}

@media screen and (max-width: 768px) {
  .form-grid ::v-deep(.el-form-item__label),
  .form-container ::v-deep(.el-form-item__label) {
    width: 80px !important;
  }

  .form-grid ::v-deep(.el-form-item__content),
  .form-container ::v-deep(.el-form-item__content) {
    margin-left: 90px !important;
  }

  /* 移动端调整最大高度 */
  .student-dialog ::v-deep(.el-dialog) {
    max-height: 90vh;
  }

  .dialog-content-wrapper {
    max-height: calc(90vh - 130px);
  }
}
</style>
